<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\FenxiaoRequest;
use App\Http\Controllers\Controller;
use EasyWeChat\Foundation\Application;

use App\Http\Model\User;
use App\Http\Model\Admin;
use App\Http\Model\Help;
use App\Http\Model\Fenxiao;
use App\Http\Model\Withdrawals;
use Config;
use Hash;
use DB;

class UserController extends Controller
{
    //
    public function userList(){
    	$user = new User;
    	$userInfo = $user->get();
    	return view("admin.user.userList", ["userInfo"=>$userInfo]);
    }
    public function userDetail($id){
    	$user = new User;
    	$userInfo = $user->find($id);
    	return view("admin.user.userDetail", ["userInfo"=>$userInfo]);
    }
    //分销列表
    public function fenxiaoList(){
        $user = new User;
        $userInfo = $user->where('identity','1')->get();
        return view("admin.user.fenxiao-list", ["userInfo"=>$userInfo]);
    }
    public function vipList(){
        $user = new User;
        $userInfo = $user->where('identity','0')->get();
        return view("admin.user.vip-list", ["userInfo"=>$userInfo]);
    }
    public function fenxiaoDetail($id){
        $user = new User;
        $userInfo = $user->find($id);
        return view("admin.user.fenxiao-detail", ["userInfo"=>$userInfo]);
    }
    public function fenxiaoSet(){
        $fenxiao = new Fenxiao;
        $fenxiaoInfo = $fenxiao->first();
        return view("admin.user.fenxiao-set", ["fenxiaoInfo"=>$fenxiaoInfo]);
    }
    public function quxiao(Request $request){
        $id = $request->input("id");
        $info = $request->input("info");
        $user = new User;
        $userInfo = $user->find($id);
        $userInfo->distributor_status = 0;
        $userInfo->identity = 0;
        $userInfo->distributor_message = $info;
        $userInfo->save();
        $this->quxiao_template($userInfo->open_id,$userInfo->distributor_message);
        echo 1;die;
    }
    public function fenxiaoSetRes(FenxiaoRequest $request){
        $fenxiao = new Fenxiao;
        // dd($request->all());
        $fenxiaoInfo = $fenxiao->find($request->input("id"));
        $fenxiaoInfo->re_code_1 = $request->input("re_code_1");
        $fenxiaoInfo->re_money_business = $request->input("re_money_business");
        $fenxiaoInfo->re_money_1 = $request->input("re_money_1");
        $fenxiaoInfo->re_money_2 = $request->input("re_money_2");
        $fenxiaoInfo->re_money_3 = $request->input("re_money_3");
        $fenxiaoInfo->re_money_4 = $request->input("re_money_4");
        $fenxiaoInfo->re_money_5 = $request->input("re_money_5");
        $fenxiaoInfo->money_1 = $request->input("money_1");
        $fenxiaoInfo->money_2 = $request->input("money_2");
        $fenxiaoInfo->money_3 = $request->input("money_3");
        $fenxiaoInfo->money_4 = $request->input("money_4");
        $fenxiaoInfo->money_5 = $request->input("money_5");
        $fenxiaoInfo->save();
        return redirect('admin/fenxiao-set')->with('success','分销设置成功');
    }
    public static function getUser($id, $identity){
        $user = new User;
        $userInfo = $user->where("pid", $id)->where("identity", $identity)->get();
        return $userInfo;die;
    }
    public static function getMoney($id){
        $user = new User;
        $userInfo = $user->find($id);
        $sum = 0;
        if(count($userInfo->complateOrders)){
            foreach ($userInfo->complateOrders as $k => $v) {
                foreach ($v->ordersInfo as $kk => $vv) {
                    $sum = $sum + $vv->price * $vv->num;
                }
            }
        }
        echo $sum;
    }
    public static function getTeamMoney($id){
        $user = new User;
        $userInfo = $user->where("pid", $id)->get();
        $sum = 0;
        if(count($userInfo)){
            foreach ($userInfo as $k => $v) {
                if(count($v->complateOrders)){
                    foreach ($v->complateOrders as $kk => $vv) {
                        foreach ($vv->ordersInfo as $kkk => $vvv) {
                            $sum = $sum + $vvv->price * $vvv->num;
                        }
                    }
                }
            }
        }
        echo $sum;
    }
    // 分销商申请
    public function fenxiaoApply(){
        $user = new User;
        $userInfo = $user->where("distributor_status", 1)->get();
        return view("admin.user.fenxiao-apply", ["userInfo"=>$userInfo]);
    }
    // 分销商申请详情
    public function fenxiaoApplyDetail($id){
        // dd(\App\Http\Controllers\Admin\UserController::getMoney(22));
        $user = new User;
        $userInfo = $user->find($id);
        return view("admin.user.fenxiao-apply-detail", ["userInfo"=>$userInfo]);
    }
    // 分销商申请通过
    public function apply(Request $request){
        $id = $request->input("id");
        $user = new User;
        $userInfo = $user->find($id);
        $userInfo->distributor_status = 3;
        $userInfo->identity = 1;
        $this->fenxiao_template($userInfo->open_id);
        $userInfo->save();
        echo 1;die;
    }
    // 分销商申请驳回
    public function applyFail(Request $request){
        $id = $request->input("id");
        $info = $request->input("info");
        $user = new User;
        $userInfo = $user->find($id);
        $userInfo->distributor_status = 2;
        $userInfo->distributor_message = $info;
        $userInfo->save();
        $this->fenxiao_fail_template($userInfo->open_id,$userInfo->distributor_message);
        echo 1;die;
    }
    // 分销商申请通过
    public function applyForm($id){
        $user = new User;
        $userInfo = $user->find($id);
        $userInfo->distributor_status = 3;
        $this->fenxiao_template($userInfo->open_id);
        $userInfo->save();
        return redirect("admin/fenxiao-apply")->with("success", "通过审核");
    }
    // 分销商申请驳回
    public function applyFailForm(Request $request){
        $id = $request->input("id");
        $info = $request->input("info");
        $user = new User;
        $userInfo = $user->find($id);
        $userInfo->distributor_status = 2;
        $userInfo->distributor_message = $info;
        $this->fail_template($userInfo->open_id);
        $userInfo->save();
        return redirect("admin/fenxiao-apply")->with("success", "驳回成功");
    }

    public function fail_template($openid){
        $app = app('wechat');
        $notice = $app->notice;
        $template_id = 'aBErbQOsdMIlFfPQZroy-IH4HCKRFJZtJPAqt1M37Lk';
        $url = Config::get('app.domain_name');
        $data = array(
            "first"=>'很遗憾您的申请未通过',
            "keyword1"=>'姓名',
            "keyword2"=>'联系方式',
            "remark"=>'请及时查看物流报告',
        );
        $res = $notice->uses($template_id)->withUrl($url)->andData($data)->andReceiver($openid)->send();
        return $res;
    }
    //帮助中心
    public function help(){
        $help = new Help;
        $helpInfo = $help->orderBy('updated_at','desc')->get();
        return view("admin.user.helpList",[
            'helpList'=>$helpInfo
        ]);
    }
    public function helpAdd(){
        return view('admin.user.helpAdd');
    }
    public function helpAddRes(Request $request){
        $help = new Help;
        $help->title = $request->input('title');
        $help->content = $request->input('content');
        $help->save();
        return redirect("/admin/help")->with("success", "新增成功");
    }
    public function helpUpdate($id){
        $help = new Help;
        $helpInfo = $help->find($id);
        return view('admin.user.helpUpdate',[
            "helpInfo" => $helpInfo
        ]);
    }
    public function helpUpdateRes(Request $request){
        $help = new Help;
        $helpInfo = $help->find($request->input('id'));
        $helpInfo->title =  $request->input('title');
        $helpInfo->content =  $request->input('content');
        $helpInfo->save();
        return redirect("/admin/help")->with("success", "修改成功");
    }
    public function helpDelete(Request $request){
        $help = new Help;
        $id = $request->input('id');
        $res = $help->where('id',$id)->delete();
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }
    //管理员登录
    public function login(){
        return view('admin.user.login');
    }
    public function loginRes(Request $request){
        $username = $request->input('username');
        $password = $request->input('password');
        $user = new Admin;
        $userInfo = $user->where('username',$username)->first();
        if($userInfo){
            if(Hash::check(md5($password),$userInfo->password)){
                session(['admin'=>$username]);
                session()->save();
                return redirect('/admin')->with("success","登录成功");
            }else{
                return back()->with("status","用户名或密码错误1!!!");
            }
        }else{
            return back()->with("status","用户名或密码错误2!!!");
        }
    }
    //修改密码
    public function updatePwd(){
        return view('admin.user.updatePwd');
    }
    public function updatePwdRes(Request $request){
        $user = new Admin;
        $oldPwd = $request->input('oldPwd');
        $newPwd = $request->input('newPwd');
        $userInfo = $user->where('username',session('admin'))->first();
        if(Hash::check(md5($oldPwd),$userInfo->password)){
            $userInfo->password = Hash::make(md5($newPwd));
            if($userInfo->save()){
                return redirect('/admin')->with('status','修改成功');
            }else{
                return back()->with('status','修改失败');
            }
        }else{
            return back()->with('status','原密码输入错误');
        }
    }
    //设置客服
    public function changeIdentity(Request $request){
        $user_id = $request->input("user_id");
        $user_type = $request->input("user_type");
        $user = new User;
        $nowTime = date("Y-m-d H:i:s");
        $result = $user->where('id', $user_id)->update(['user_type' => $user_type, 'updated_at'=>$nowTime]);
        if($result){
            echo 1;die;
        }else{
            echo 0;die;
        }
    }
    // 提现设置
    public function withdrawalsSet(){
        $fenxiao = new Fenxiao;
        $fenxiaoInfo = $fenxiao->first();
        return view("admin.user.withdrawals-set", ["fenxiaoInfo"=>$fenxiaoInfo]);
    }
    // 提现设置处理
    public function withdrawalsSetRes(Request $request){
        $fenxiao = new Fenxiao;
        $id = $request->input("id");
        $fenxiaoInfo = $fenxiao->find($id);
        $fenxiaoInfo->min_money = $request->input("min_money");
        $fenxiaoInfo->save();
        return redirect("admin/withdrawals-set")->with("success", "提现金额设置成功");
    }
    // 提现列表
    public function withdrawalsList(){
        $withdrawals = new Withdrawals;
        $withdrawalsInfo = $withdrawals->selectRaw("withdrawals.*,sum(money) as allMoney")->where("status","1")->groupBy("user_id")->get();
        return view("admin.user.withdrawals-list", ["withdrawalsInfo"=>$withdrawalsInfo]);
    }
    // 提现申请
    public function withdrawalsApply(){
        $withdrawals = new Withdrawals;
        $withdrawalsInfo = $withdrawals->where("status","0")->get();
        return view("admin.user.withdrawals-apply", ["withdrawalsInfo"=>$withdrawalsInfo]);
    }
    // 提现成功处理
    public function withdrawalsSuccess(Request $request){
        $withdrawals = new Withdrawals;
        $id = $request->input("id");
        $withdrawalsInfo = $withdrawals->find($id);
        $withdrawalsInfo->status = 1;
        $user_id = $withdrawalsInfo->user_id;
        $user_money = $withdrawalsInfo->money;
        $withdrawalsInfo->updated_at = date("Y-m-d H:i:s");
        $user = new User;
        $userInfo = $user->find($user_id);
        $ye = (float)$userInfo->money - (float)$user_money;
        if($ye >=0){
            $userInfo->money = $ye;
            $userInfo->save();
            $withdrawalsInfo->save();
            echo 1;die;
        }else{
            echo 0;die;
        }
    }
    // 提现驳回处理
    public function withdrawalsError(Request $request){
        $withdrawals = new Withdrawals;
        $id = $request->input("id");
        $error_message = $request->input("info");
        $withdrawalsInfo = $withdrawals->find($id);
        $withdrawalsInfo->status = 2;
        $withdrawalsInfo->error_message = $error_message;
        $withdrawalsInfo->updated_at = date("Y-m-d H:i:s");
        $withdrawalsInfo->save();
        echo 1;die;
    }
    // 提现成功列表
    public function withdrawalsYes(){
        $withdrawals = new Withdrawals;
        $withdrawalsInfo = $withdrawals->where("status","1")->get();
        return view("admin.user.withdrawals-yes", ["withdrawalsInfo"=>$withdrawalsInfo]);
    }
    // 提现失败列表
    public function withdrawalsNo(){
        $withdrawals = new Withdrawals;
        $withdrawalsInfo = $withdrawals->where("status","2")->get();
        return view("admin.user.withdrawals-no", ["withdrawalsInfo"=>$withdrawalsInfo]);
    }
}
